package com.zscat.mallplus.dms.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zscat.mallplus.dms.service.IDeviceService;
import com.zscat.mallplus.dms.service.IDeviceTaskService;
import com.zscat.mallplus.dms.service.IDmsOrderService;
import com.zscat.mallplus.dms.service.IRoomService;
import com.zscat.mallplus.enums.OrderStatus;
import com.zscat.mallplus.enums.TaskType;
import com.zscat.mallplus.qsg.BilliardWeighService;
import com.zscat.mallplus.qsg.QsgDevice;
import com.zscat.mallplus.qsg.QsgDeviceTask;
import com.zscat.mallplus.qsg.QsgRoom;
import com.zscat.mallplus.qsg.impl.MqttConfigService;
import com.zscat.mallplus.qsg.mapper.QsgRoomMapper;
import com.zscat.mallplus.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class RoomServiceImpl extends ServiceImpl<QsgRoomMapper, QsgRoom> implements IRoomService {

    @Autowired
    private MqttConfigService mqttConfigService;

    @Autowired
    private BilliardWeighService weighService;

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private IDmsOrderService orderService;

    @Override
    public void closeNewDevice(QsgDevice device) throws MqttException {
        if(0 == device.getIsWeigh()){
            weighService.close(device.getDeviceCode());
        }else{
            JSONObject data1 = new JSONObject();
            JSONObject header1 = new JSONObject();
            JSONObject body1 = new JSONObject();
            header1.put("Code","1004");
            header1.put("Time", System.currentTimeMillis());
            header1.put("Sign","lvzaina");
            data1.put("Header",header1);
            body1.put("Delay",0);
            body1.put("Cmd","Order");
            body1.put("MsgId",1683557943);
            body1.put("Lock",0);
            body1.put("Type",0);
            body1.put("ID",0);
            JSONArray node = new JSONArray();
            node.add(1);
            node.add(2);
            body1.put("Node",node);
            data1.put("Body",body1);
            mqttConfigService.publish("topic/receive/"+ device.getDeviceCode(), data1.toJSONString());
        }
    }

    @Override
    public void openRoomDoor(QsgDevice device) throws MqttException {
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                try {
                    JSONObject data = new JSONObject();
                    JSONObject header = new JSONObject();
                    JSONObject body = new JSONObject();
                    header.put("Code", "1004");
                    header.put("Time", System.currentTimeMillis());
                    header.put("Sign", "lvzaina");
                    data.put("Header", header);
                    body.put("Delay", 3);
                    body.put("Cmd", "RemoteOpenDoor");
                    body.put("MsgId", 1682220193);
                    body.put("Lock", 189);
                    body.put("Type", 0);
                    body.put("ID", 0);
                    data.put("Body", body);
                    mqttConfigService.publish("topic/receive/" + device.getDeviceCode(), data.toJSONString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        timer.schedule(timerTask,5000);
    }

    @Override
    public void operateAirControl(String airDeviceCode,Integer controlType){
        JSONObject data1 = new JSONObject();
        JSONObject header1 = new JSONObject();
        JSONObject body1 = new JSONObject();
        header1.put("Code","1004");
        header1.put("Time", System.currentTimeMillis());
        header1.put("Sign","lvzaina");
        data1.put("Header",header1);
//        body1.put("AreaID",1);
        body1.put("Cmd","AirCtrl");
        body1.put("MsgId",1683557943);
        body1.put("On",controlType);
        body1.put("Mode",0);
        body1.put("Temp",26);
        body1.put("WindSpeed",0);
        body1.put("WindEnable",0);
        data1.put("Body",body1);
        try {
            mqttConfigService.publish("topic/receive/"+ airDeviceCode,data1.toJSONString());
        } catch (MqttException e) {
            log.error("设备ID:{}控制空调失败.异常:{}",airDeviceCode,e.getMessage());
            e.printStackTrace();
        }
    }

    @Override
    public void closeAirControl(QsgDevice device) {
        int isAir = device.getIsAirCondition().intValue();
        if(isAir == 0){
            String airDeviceCode = device.getAirDeviceCode();
            QueryWrapper qwone = new QueryWrapper();
            qwone.eq("air_device_code",airDeviceCode);
            List<QsgDevice> devices = deviceService.list(qwone);
            List<Long> deviceIds = new ArrayList<>();
            for (QsgDevice d: devices
            ) {
//                if(d.getId().longValue() == device.getId().longValue()){
//                    continue;
//                }
                deviceIds.add(d.getId());
            }
            QueryWrapper qworder = new QueryWrapper();
            qworder.in("device_id",deviceIds);
            qworder.eq("order_status", OrderStatus.HAS_PAYMONEY.getValue());
            qworder.le("start_time", DateUtil.dateToString(new Date(), "yyyy/MM/dd HH:mm:ss"));
            qworder.ge("end_time", DateUtil.dateToString(new Date(), "yyyy/MM/dd HH:mm:ss"));
            int usingNum = orderService.count(qworder);
            if(usingNum == 0){
                operateAirControl(airDeviceCode,0);
            }
        }
    }
}
